package dhm.com.hpx.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import dhm.com.hpx.R;
import dhm.com.hpx.adapter.MessageArticleadapter;
import dhm.com.hpx.adapter.MessageVideoadapter;
import dhm.com.hpx.base.BaseActivity;
import dhm.com.hpx.base.Presenter.PressenterImpl;
import dhm.com.hpx.base.netWork.Constant;
import dhm.com.hpx.base.netWork.LoginContract;
import dhm.com.hpx.entity.Bean;
import dhm.com.hpx.entity.FabulousArticleBean;
import dhm.com.hpx.entity.FabulousVideoBean;
import dhm.com.hpx.utils.SpUtils;

public class LikeActivity extends BaseActivity implements LoginContract.IView {

    @BindView(R.id.edit)
    TextView edit;
    @BindView(R.id.text_like)
    TextView textLike;
    @BindView(R.id.line_like)
    TextView lineLike;
    @BindView(R.id.text_liked)
    TextView textLiked;
    @BindView(R.id.line_liked)
    TextView lineLiked;
    @BindView(R.id.recy)
    XRecyclerView recy;

    private PressenterImpl pressenter;

    int page=1;
    private String uid;

    @Override
    protected int getLayout() {
        return R.layout.activity_like;
    }

    @Override
    protected void initView() {
        ButterKnife.bind(this);
        pressenter=new PressenterImpl();
        pressenter.attachView(this);

        uid = SpUtils.getString(this, "uid");

        LinearLayoutManager manager=new LinearLayoutManager(this);
        manager.setOrientation(LinearLayoutManager.VERTICAL);

        recy.setLayoutManager(manager);

        recy.setLoadingMoreEnabled(true);
        recy.setPullRefreshEnabled(true);
        page=1;
        getLikeVideo();

    }

    @Override
    protected void initData() {

    }

    private void getLikeVideo() {
        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getLikeVideo();
            }

            @Override
            public void onLoadMore() {
                getLikeVideo();
            }
        });


        Map<String,String> map=new HashMap();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("page",page+"");

        pressenter.sendMessage(this,Constant.listVideoQuery_fablulous,map, FabulousVideoBean.class);
    }


    private void getLikeArticle() {

        recy.setLoadingListener(new XRecyclerView.LoadingListener() {
            @Override
            public void onRefresh() {
                page=1;
                getLikeArticle();
            }

            @Override
            public void onLoadMore() {
                getLikeArticle();
            }
        });


        Map<String,String> map=new HashMap();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("page",page+"");

        pressenter.sendMessage(this,Constant.listArticlesQuery_fablulous,map, FabulousArticleBean.class);

    }

    String type="2";

    private void clearlike() {
        Map<String,String> map=new HashMap();
        map.put("token", Constant.TOKEN);
        map.put("uid",uid);
        map.put("type",type);
        pressenter.sendMessage(this,Constant.fabulousclear_del,map, Bean.class);
    }


    @SuppressLint("NewApi")
    @OnClick({R.id.back, R.id.edit, R.id.video, R.id.article})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.back:
                finish();
                break;
            case R.id.edit:
                clearlike();
                break;
            case R.id.video:
                type="2";
                textLike.setTextColor(getResources().getColor(R.color.main));
                textLiked.setTextColor(Color.parseColor("#666666"));
                lineLike.setVisibility(View.VISIBLE);
                lineLiked.setVisibility(View.INVISIBLE);
                page=1;
                getLikeVideo();
                break;
            case R.id.article:
                type="1";
                textLiked.setTextColor(getResources().getColor(R.color.main));
                textLike.setTextColor(Color.parseColor("#666666"));
                lineLiked.setVisibility(View.VISIBLE);
                lineLike.setVisibility(View.INVISIBLE);
                page=1;
                getLikeArticle();
                break;
                default:
        }
    }

    private List<FabulousVideoBean.DataBean> fabulousVideo;
    private MessageVideoadapter messageVideoadapter;

    private List<FabulousArticleBean.DataBean> fabulousArticle;
    private MessageArticleadapter messageArticleadapter;
    @Override
    public void requesta(Object data) {
        if (data instanceof FabulousVideoBean){
            FabulousVideoBean listBean= (FabulousVideoBean) data;
            if (listBean.getCode()==1){
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    fabulousVideo = listBean.getData();
                    messageVideoadapter = new MessageVideoadapter(this);
                    messageVideoadapter.setdeleteClick(new MessageVideoadapter.OnClick() {
                        @Override
                        public void item(int posttion) {
                            if (fabulousVideo.get(posttion).getFabulousType()==2){
                                Intent intent=new Intent(LikeActivity.this, VideoActivity.class);
                                intent.putExtra("video_id",fabulousVideo.get(posttion).getVideoId()+"");
                                intent.putExtra("coverImg",fabulousVideo.get(posttion).getCoverImg()+"");
                                startActivity(intent);
                            }else {
                                Intent intent=new Intent(LikeActivity.this, ExtensionradioActivity.class);
                                intent.putExtra("extension_id",fabulousVideo.get(posttion).getVideoId()+"");
                                startActivity(intent);
                            }

                        }
                    });
                    messageVideoadapter.setaddClick(new MessageVideoadapter.OnIntentClick() {
                        @Override
                        public void item(int position) {
                            Map<String,String> map=new HashMap<>();
                            map.put("token",Constant.TOKEN);
                            map.put("uid",uid);
                            map.put("type","2");
                            map.put("cid",fabulousVideo.get(position).getFabulousId()+"");

                            pressenter.sendMessage(LikeActivity.this,Constant.fabulouscancel,map, Bean.class);
                            fabulousVideo.remove(position);
                            messageVideoadapter.setList(fabulousVideo);
                        }
                    });
                    recy.setAdapter(messageVideoadapter);
                }else {
                    fabulousVideo.addAll(listBean.getData());
                }
                messageVideoadapter.setList(listBean.getData());
                page++;
            }else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(this, listBean.getMessage(), Toast.LENGTH_SHORT).show();
            }

            recy.refreshComplete();
            recy.loadMoreComplete();

        }else if (data instanceof FabulousArticleBean){
            FabulousArticleBean listBean= (FabulousArticleBean) data;
            if (listBean.getCode()==1){
                if (page==1){
                    recy.setVisibility(View.VISIBLE);
                    fabulousArticle = listBean.getData();
                    messageArticleadapter = new MessageArticleadapter(this);
                    messageArticleadapter.setdeleteClick(new MessageArticleadapter.OnClick() {

                        @Override
                        public void item(int posttion) {
                            if (fabulousArticle.get(posttion).getFabulousType()==1){
                                Intent intent=new Intent(LikeActivity.this, ArticleActivity.class);
                                intent.putExtra("article_id",fabulousArticle.get(posttion).getArticleId()+"");
                                intent.putExtra("coverImg",fabulousArticle.get(posttion).getCoverImg()+"");
                                startActivity(intent);
                            }else {
                                Intent intent=new Intent(LikeActivity.this, ExtensionarticleActivity.class);
                                intent.putExtra("extension_id",fabulousArticle.get(posttion).getArticleId()+"");
                                startActivity(intent);
                            }


                        }
                    });
                    messageArticleadapter.setaddClick(new MessageArticleadapter.OnIntentClick() {
                        @Override
                        public void item(int position) {
                            Map<String,String> map=new HashMap<>();
                            map.put("token",Constant.TOKEN);
                            map.put("uid",uid);
                            map.put("type","1");
                            map.put("cid",fabulousArticle.get(position).getFabulousId()+"");

                            pressenter.sendMessage(LikeActivity.this,Constant.fabulouscancel,map,Bean.class);
                            fabulousArticle.remove(position);
                            messageArticleadapter.setList(fabulousArticle);
                        }
                    });


                    recy.setAdapter(messageArticleadapter);
                }else {
                    fabulousArticle.addAll(listBean.getData());
                }
                messageArticleadapter.setList(listBean.getData());
                page++;
            }else {
                if (page==1){
                    recy.setVisibility(View.GONE);
                }
                Toast.makeText(this, listBean.getMessage(), Toast.LENGTH_SHORT).show();
            }
            recy.refreshComplete();
            recy.loadMoreComplete();
        }else if (data instanceof Bean){
            Bean bean= (Bean) data;
            Toast.makeText(this, bean.getMessage(), Toast.LENGTH_SHORT).show();
            if (bean.getCode()==1){
                if (bean.getMessage().equals("清空成功")){
                    fabulousVideo.clear();
                    fabulousArticle.clear();
                    messageArticleadapter.setList(fabulousArticle);
                    messageVideoadapter.setList(fabulousVideo);
                }
            }
        }
    }

    @Override
    public void fail(String error) {

    }
}
